//
//  MyCryptManager.swift
//
//
//  Created by lvzhao on 2024/5/25.
//

import Foundation

// MARK - 对外暴露的接口, 用户只可以使用这两个接口, 进行数据的加解密

public protocol ICryptManager {
    
    
    /// - 加密的函数
    /// - Parameters:
    ///   - key: 加密的秘钥
    ///   - strToEncode: 加密的字符串
    ///   - type: 加密的类型
    /// - Returns: 加密后生成的字符串
    func encrypt(key: String?, strToEncode: String?, type: MyEncryptType) -> String?
    
    /// 解密的函数
    /// - Parameters:
    ///   - key: 解密的秘钥
    ///   - strToDecode: 解密的字符串
    ///   - type: 解密的类型
    /// - Returns: 解密后生成的字符串
    func decrypt(key: String?, strToDecode: String?, type: MyEncryptType) -> String?
}


open class MyCryptManager: ICryptManager {
    
    //实现一个加解密的协议的对象
    public var cryptBehavior: MyCryptBehavior?
    
    public func encrypt(key: String?, strToEncode: String?, type: MyEncryptType) -> String? {
        switch type {
        case .aes:
            cryptBehavior = MyAESCryptBehavior()
        case .other:
            return strToEncode

        }
        guard let crypt = cryptBehavior else { return nil }
        
        return crypt.encrypt(key: key, strToEncode: strToEncode)
    }
    
    public func decrypt(key: String?, strToDecode: String?, type: MyEncryptType) -> String? {
        
        switch type {
        case .aes:
            cryptBehavior = MyAESCryptBehavior()
        case .other:
            // TODO: 后面可以扩展新的加密方式
            print()
        }
        guard let crypt = cryptBehavior else {return nil}
        return crypt.decrypt(key: key, strToDecode: strToDecode)
    }
    
    
    
    
    
}
